﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.IO;

namespace ProjetoFase1
{
    public partial class FormLogin : Form
    {
        public FormLogin()
        {   
            // initialize the form
            InitializeComponent();
            txtUsername.Focus();
        }

        private void FormLogin_Close()
        {
            SyncDB.dropSubscription();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtUsername.Text = "";
            txtPassword.Text = "";
            txtUsername.Focus();
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            bool validLogin;
            btnEnter.Enabled = false;

            // loads the publication with the user login
            try
            {
                SyncDB.createSubscription();
                SyncDB.synchronizeDatabase(txtUsername.Text, txtPassword.Text);

                // check if the login information is valid
                validLogin = validateLogin();
            }
            catch (Exception ex)
            {
                validLogin = false;
            }

            // if it is then load the info
            if (validLogin)
            {
                UserLogin.Username = txtUsername.Text;
                UserLogin.Password = txtPassword.Text;
                // load the new form to manage activities
                FormManageActivities form = new FormManageActivities();
                // show the next form
                form.Show();

                // returns from a click to logout
                // drops the subscription
                //SyncDB.dropSubscription();
                // sets the text field to default values
                txtUsername.Text = "";
                txtPassword.Text = "";
            }
            else
            {
                DialogLogin form = new DialogLogin();
                form.Show();
                // drops the subscription
                SyncDB.dropSubscription();
                txtUsername.Text = "";
                txtPassword.Text = "";
            }
            btnEnter.Enabled = true;
        }

        private bool validateLogin()
        {
            SqlCeConnection cn = null;

            try {

                cn = new SqlCeConnection("Data Source=\\My Documents\\" +
                    "g21.sdf;" +
                    "SSCE:Database Password=g21");
                SqlCeCommand cmd = cn.CreateCommand();
                cmd.CommandText = "select count(*) from technician where id='"+txtUsername.Text+
                    "' and password='"+txtPassword.Text+"'";
                cn.Open();

                // reads the select and finds if there is an user
                SqlCeDataReader dr = cmd.ExecuteReader();
                dr.Read();
                int userFound = dr.GetInt32(0);

                // closes the connection
                cn.Close();

                // returns the result
                if (userFound > 0)
                    return true;
                else
                    return false;
            }
            catch (SqlCeException sqlex)
            {
                foreach (SqlCeError sqlError in sqlex.Errors)
                {
                    MessageBox.Show(sqlError.Message, "Error");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
            finally
            {
                if (cn.State != ConnectionState.Closed)
                {
                    cn.Close();
                }
            }

            return false;
        }
    }
}